<?php
/***********************************************************************
 * @package     IvanEngine
 * @subpackage  Framwork
 * @author      Ivan <ivanzx@msn.com>
 * @created     2010-05-31
 ***********************************************************************/
if(defined("IVANENGINE_FRAMEWORK_ENGINE"))
    die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_FRAMEWORK_ENGINE", true);

require_once (dirname(__FILE__).'/Common.php');
require_once (dirname(__FILE__).'/WindowManager.php');
require_once (dirname(__FILE__).'/PluginManager.php');
require_once (dirname(__FILE__).'/ThemesManager.php');
require_once (dirname(__FILE__).'/Context.php');
require_once (dirname(__FILE__).'/Config/Config.php');

class CEngine extends CEventSingleton
{
    const EVT_PRE_SHUTDOWN  = 'EVT_PRE_SHUTDOWN';
    const EVT_POST_SHUTDOWN = 'EVT_SHUTDOWN';
    
    static protected $m_this= null;
    
    public function __construct()
    {
        parent::__construct();
        $this->AddEvent(self::EVT_PRE_SHUTDOWN);
        $this->AddEvent(self::EVT_POST_SHUTDOWN);
    }
    
    public function __destruct()
    {
        parent::__destruct();
    }
    
    static public function Get()
    {
        if( self::$m_this == null )
        {
            parent::StartSingleton();
            self::$m_this = new CEngine();
            parent::EndSingleton();
        }
        return self::$m_this;
    }
    
    public function Start()
    {
        try
        {
            $this->Initialize();
            $this->Shutdown();
        }
        catch (CException $e)
        {
            $e->PushToLog();
            $this->Shutdown();
        }
    }
    
    public function Initialize()
    {
        register_shutdown_function(array($this, "PreShutdown"));
        
        // Initialize all manager
        //-------------------------------------------------------------------
        CCacheManager::Get()->Initialize(CConfig::Option(CConfig::OT_CachePath));
        
        CConfig::Get()->Initialize();
        
        CSessionStorage::Get()->Initialize(array(
            CSessionStorage::OT_Session_Cookie_Lifetime => 1800,
            CSessionStorage::OT_AutoShutdown    => false,
            CSessionStorage::OT_Session_Name    => 'IvanEngine'));
            
        CThemesManager::Get()->Initialize();
        
        CI18NManager::Get()->Initialize(CConfig::Option(CConfig::OT_Language));
        
        CPrinter::Get()->Initialize();
        
        CContext::Get()->Initialize($this);
        
        CWindowManager::Get()->Initialize();
        
        if( !CPluginManager::Get()->Initialize() ){
            return false;
        }
        
        CContext::Get()->Dispatch();
        
        return true;
    }
    
    public function Shutdown()
    {
        register_shutdown_function(array($this, "PostShutdown"));
        exit();
    }
    
    public function PreShutdown()
    {
        $evtArgs    = new CEventArgs();
        $evtArgs->object    = $this;
        $this->FireEvent(self::EVT_PRE_SHUTDOWN, $evtArgs);
        unset($evtArgs);
        // if it not have any body process, then show the message.
        if( CLog::Get()->Size(CLog::Get()->Level()) > 0 ){
            CPrinter::Get()->GetRoot()->AddContent(CLog::Get()->Message());
        }
    }
    
    public function PostShutdown()
    {
        $evtArgs    = new CEventArgs();
        $evtArgs->object    = $this;
        $this->FireEvent(self::EVT_POST_SHUTDOWN, $evtArgs);
        unset($evtArgs);
        CPrinter::Get()->Render();
        CResponse::Get()->Send();
        CSingleton::ReleaseAllSingleton();
    }
}
?>
